package main

import (
	"crypto/tls"
	"log"

	"gitee.com/clin003/email"
)

const (
	// SMTP设置
	// 服务器名称: smtp.office365.com
	// 端口: 587
	// 加密方法: STARTTLS
	Smtp_Outlook = "smtp.office365.com:587"
	SMTPHost     = "smtp.office365.com"
	SMTPPort     = ":587"
	SMTPUsername = "xxx@live.com"
	SMTPPassword = "xxx"
)

func main() {
	// 普通25端口Auth
	// auth := smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
	// 简单的验证Auth：适配 outlook
	auth := email.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
	//TLS 验证
	t := &tls.Config{ServerName: SMTPHost, InsecureSkipVerify: true}
	e := email.NewEmail()
	// e.From="baicailin@live.com"
	// e.From = "baicai <baicailin@live.com>"
	e.From = SMTPUsername
	e.To = []string{"hi@lyhuilin.com"}
	e.Subject = "测试"
	e.Text = []byte("Hello World!")
	_, err := e.AttachFileURL("https://cdn.jsdelivr.net/gh/clin003/cdn/assets/images/baicai.jpeg")
	if err != nil {
		log.Print(err)
	}
	// err := e.Send(SMTPHost+SMTPPort, auth)
	err = e.SendWithStartTLS(SMTPHost+SMTPPort, auth, t)
	if err != nil {
		log.Fatal(err)
	}
}

// // with pool
// func main() {
// 	// 普通25端口Auth
// 	// auth := smtp.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
// 	// 简单的验证Auth：适配 outlook
// 	auth := email.PlainAuth("", SMTPUsername, SMTPPassword, SMTPHost)
// 	//TLS 验证
// 	t := &tls.Config{ServerName: SMTPHost, InsecureSkipVerify: true}
// 	p, err := email.NewPool(SMTPHost+SMTPPort, 5, auth, t)
// 	if err != nil {
// 		log.Fatal(err)
// 	}

// 	e := email.NewEmail()
// 	// e.From="baicailin@live.com"
// 	// e.From = "baicai <baicailin@live.com>"
// 	e.From = SMTPUsername
// 	e.To = []string{"hi@lyhuilin.com"}
// 	e.Subject = "测试2" + time.Now().String()
// 	// e.Text = []byte("Hello World!")
// 	_, err = e.AttachFileURL("https://cdn.jsdelivr.net/gh/clin003/cdn/assets/images/baicai.jpeg")
// 	if err != nil {
// 		log.Print(err)
// 	}

// 	err = p.Send(e, 10*time.Second)
// 	if err != nil {
// 		log.Fatal(err)
// 	}
// }
